/**
 * 添加注册全局变量，给三方扩展的js使用，兼容之前的旧逻辑，内部代码禁止使用
 */

import {useBaseInfoStore} from '@/store/modules/base-info.js';

export default function () {
    let businessData = useBaseInfoStore().baseInfo?.businessData || {};
    //写入全局变量
    const {affair, template, summary} = businessData;
    //协同id
    window.summaryId = summary.id;
    //事项的id
    window.affairId = affair.id;
    //节点权限，系统内置的就是标识代码，比如newCol\collaboration，自定义的为名称
    window.nodePolicy = affair.nodePolicy;
    //流程id
    window.processId = affair.processId;
    //cap4有流程表单的数据记录id
    window.fromRecordId = affair.formRecordid;
    window.formRecordid = affair.formRecordid;
    //工作流的示例id
    window.wfCaseId = affair.caseId;
    //协同标题
    window.subject = affair.subject;
    //cap4有流程表单的id
    window.formAppId = summary.formAppid;
    //工作流节点id
    window.wfActivityId = affair.activityId;
    window._summaryActivityId = affair.activityId;
    window._affairMemberId=affair.memberId;
    window.senderId=affair.senderId;
    window.nodePolicy=affair.nodePolicy;
    window.DR = useBaseInfoStore().baseInfo?.dataRelation?.id;
    window.affairState = affair.state;
    window.projectId = summary.projectId;
    window.workflowAdvanced = true;
    //工作流流程图模版id
    window.templateWorkflowId = template?.workflowId;
    //TODO 展示写死 0 ： 没有做修改, 1:修改了正文   2：修改了附件 3 ： 修改了正文和附件
    window.isEditAttOrContent = 0;
    //协同所使用的模版id
    window.templateId = template?.id;
    //嵌入正文组件需要读取，否则高度展示异常
    window.bIsContentNewPage = true;
    //最后加载插件资源
    loadPluginResource();
    const {currentUser} = businessData;
    //挂载全局用户
    $.ctx.CurrentUser=currentUser;
}

/**
 * 加载插件js/css资源到root节点
 */
function loadPluginResource() {
    //加载插件扩展的js  
    let other = useBaseInfoStore().baseInfo?.businessData?.other || {};
    const {pluginJsFiles, pluginCssFiles} = other;
    if (pluginJsFiles && pluginJsFiles.length > 0) {
        pluginJsFiles.forEach(item => {
            if (item.indexOf('{serverRoot}') === -1) {
                item = `{serverRoot}/${item}`
            }
            let script = document.createElement("script");
            script.src = item.replace('{serverRoot}', `${_ctxPath}`) + window._resSuffix;
            document.body.appendChild(script);
        })
    }
    if (pluginCssFiles && pluginCssFiles.length > 0) {
        pluginCssFiles.forEach(item => {
            let link = document.createElement("link");
            link.href = item.replace('{serverRoot}', `${_ctxPath}`) + window._resSuffix;
            link.rel = "stylesheet";
            document.head.appendChild(link);
        })
    }
    //人脸识别应该在自己的方法里面绑定事件
    if (pluginJsFiles) {
        let includes = pluginJsFiles.some(item=>item.includes('faceIdQrCode.js'));
        if (includes){
            //绑定
            $.ctp.bind("beforeSubmit", function (config) {
                function successFun(result) {
                    //设置后端校验是否人脸通过的参数
                    let otherParamData = useBaseInfoStore().otherParamData;
                    otherParamData.data.qrCodeId=result.qrCodeId;
                    otherParamData.data.bizInfoToken=result.bizInfoToken;
                    config.success();
                }
                window.faceEECheck(false, successFun, config.error);
            });
        }
    }
}